package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    //新增套餐，同时需要保存套餐和菜品之间的关联关系
    @Override
    @Transactional  //开启事务
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal表,执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((itm) -> {
            itm.setSetmealId(setmealDto.getId());
            return itm;
        }).collect(Collectors.toList());

        //保存套餐和菜品的基本信息，操作setmealDish表，执行inset操作
        setmealDishService.saveBatch(setmealDishes);
    }


    //删除套餐，同时删除套餐和菜品的关联数据
    @Override
    @Transactional  //开启事务
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确实是否停售
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);
        int count = this.count(lqw);
        if (count > 0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //删除套餐中数据
        this.removeByIds(ids);


        //删除关系表中数据(收到的ids为套餐的id，而不是菜品关系表的id，不能直接调用removeByIds)
        //delete form setmeal dish where setmeal id in (1,2,3)

        LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(SetmealDish::getSetmealId ,ids);

        setmealDishService.remove(lqw1);

    }

    // TODo 查询套餐，同时查询套餐，分类和菜品表
    @Override
    @Transactional  //开启事务
    public SetmealDto getWithCategoryWithSetmealDish(Long id) {
        //创建SermealDto对象接收数据
        SetmealDto setmealDto = new SetmealDto();

        //根据id查询Setmeal表
        Setmeal byId = this.getById(id);

        //拷贝数据
        BeanUtils.copyProperties(byId,setmealDto);

        //创建表达式查询category数据
        Category byId1 = categoryService.getById(byId.getCategoryId());
        String name = byId1.getName();

        setmealDto.setCategoryName(name);

        //查询菜品关系表
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);

        //进行搜索
        List<SetmealDish> list = setmealDishService.list(lqw);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    // TODO 修改套餐，同时操作套餐和菜品表
    @Override
    @Transactional      //添加事务注解保证数据一致
    public void updateWithCategoryWithSermealDish(SetmealDto setmealDto) {
        //修改当前套餐表中数据
        this.updateById(setmealDto);

        //修改菜品表数据
        //删除表中数据
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);

        //重新添加数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((itm) -> {
            itm.setSetmealId(setmealDto.getId());
            return itm;
        }).collect(Collectors.toList());


        setmealDishService.saveBatch(setmealDishes);
//        setmealDishService.updateBatchById(setmealDishes);

    }
}
